var arrDefaultEmotion= new Array();
arrDefaultEmotion = ["http://public.blogbus.com/biaoqing/163/23.gif","http://public.blogbus.com/biaoqing/163/22.gif","http://public.blogbus.com/biaoqing/163/21.gif","http://public.blogbus.com/biaoqing/163/19.gif","http://public.blogbus.com/biaoqing/163/17.gif","http://public.blogbus.com/biaoqing/163/13.gif","http://public.blogbus.com/biaoqing/163/10.gif","http://public.blogbus.com/biaoqing/163/9.gif","http://public.blogbus.com/biaoqing/163/7.gif","http://public.blogbus.com/biaoqing/163/1.gif","http://public.blogbus.com/biaoqing/163/3.gif","http://public.blogbus.com/biaoqing/163/5.gif"];

function $v2(id) {
	return document.getElementById(id);
}

function insertEmotion() {
	if (!$v2("commentForm")) return;	
	var arrTemp = new Array();
	if (arrEmotion.length != 0 ) {
		//var emotionList = "";
		for (var i = 0; i < arrEmotion.length; i++) {
			var emotionList = "<img src='";
			emotionList += arrEmotion[i];
			emotionList += "' onClick='addEm(\"[cusFace:";
			emotionList += i;
			emotionList += "]\")' />";
			arrTemp.push(emotionList);
		}
	} else {
		//var emotionList = "";
		for (var i = 0; i < arrDefaultEmotion.length; i++) {
			var emotionList = "<img src='";
			emotionList += arrDefaultEmotion[i];
			emotionList += "' onClick='addEm(\"[defFace:";
			emotionList += i;
			emotionList += "]\")' />";
			arrTemp.push(emotionList);
		}
	}
	var divEmList = document.createElement("div");
	//divEmList.setAttribute("id","emList");
	divEmList.id = "emList";
	divEmList.innerHTML = arrTemp.join("");
	$v2("commentForm").appendChild(divEmList);
	//alert(emotionList);
}
function replaceEmotion() {
	if ($v2("comments")) {
		if (arrEmotion.length != 0 ) {
		$v2("comments").innerHTML = $v2("comments").innerHTML.replace(/\[cusFace:(\d+)\]/g,function(a,b,c,d){return "<img src='" + arrEmotion[b] + "' />";});
		$v2("comments").innerHTML = $v2("comments").innerHTML.replace(/\[defFace:(\d+)\]/g,function(a,b,c,d){return "<img src='" + arrDefaultEmotion[b] + "' />";});
		} else {
		$v2("comments").innerHTML = $v2("comments").innerHTML.replace(/\[defFace:(\d+)\]/g,function(a,b,c,d){return "<img src='" + arrDefaultEmotion[b] + "' />";});
		$v2("comments").innerHTML = $v2("comments").innerHTML.replace(/\[cusFace:\d+\]/g,"");
	}
	}
	if ($v2("nComments")) {$v2("nComments").innerHTML = $v2("nComments").innerHTML.replace(/\[\w+Face:\d+\]/g,"");}
}

function addEm(code) {
	var f = document.forms;
	for (var i = 0; i <= f.length; i++) {
		if (f[i].checksum) {
			f[i].content.value += code;f[i].content.focus();
			return;
		}
	}
}

if (document.addEventListener) {
	window.addEventListener('load',insertEmotion,false);
	window.addEventListener('load',replaceEmotion,false);
} else if (window.attachEvent) {
	window.attachEvent('onload',insertEmotion);
	window.attachEvent('onload',replaceEmotion);
} else {
	window.onload = function () {
	insertEmotion();
	replaceEmotion();
	//alert("hi,jiee");
	}
}
